package com.example.demo.mapper;

import com.example.demo.entity.Course;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 课程信息表数据访问接口
 * 使用 MyBatis 注解
 */
@Mapper
public interface CourseMapper {

    // --- 插入 (Insert) ---
    @Insert("INSERT INTO courses (course_id, course_name, description, semester_id, teacher_id, total_lessons, png) " +
            "VALUES (#{courseId}, #{courseName}, #{description}, #{semesterId}, #{teacherId}, #{totalLessons}, #{png})")
    int insertCourse(Course course);

    // --- 查询单个 (Select by ID) ---
    @Select("SELECT * FROM courses WHERE course_id = #{courseId}")
    @Results(id = "courseResultMap", value = {
            @Result(property = "courseId", column = "course_id", id = true),
            @Result(property = "courseName", column = "course_name"),
            @Result(property = "description", column = "description"),
            @Result(property = "createdAt", column = "created_at"),
            @Result(property = "semesterId", column = "semester_id"),
            @Result(property = "teacherId", column = "teacher_id"),
            @Result(property = "totalLessons", column = "total_lessons"),
            @Result(property = "png", column = "png")
    })
    Course selectCourseById(@Param("courseId") String courseId);

    // --- 查询全部 (Select All) ---
    @Select("SELECT * FROM courses")
    @ResultMap("courseResultMap")
    List<Course> selectAllCourses();

    // --- 更新 (Update) ---
    @Update("UPDATE courses SET " +
            "course_name = #{courseName}, " +
            "description = #{description}, " +
            "semester_id = #{semesterId}, " +
            "teacher_id = #{teacherId}, " +
            "total_lessons = #{totalLessons}, " +
            "png = #{png} " +
            "WHERE course_id = #{courseId}")
    int updateCourse(Course course);

    // --- 删除 (Delete) ---
    @Delete("DELETE FROM courses WHERE course_id = #{courseId}")
    int deleteCourseById(@Param("courseId") String courseId);

    // --- 根据教师ID查询课程列表 ---
    @Select("SELECT * FROM courses WHERE teacher_id = #{teacherId}")
    @ResultMap("courseResultMap")
    List<Course> selectCoursesByTeacherId(@Param("teacherId") Integer teacherId);

    // --- 新增: 根据学期ID查询课程列表 ---
    @Select("SELECT * FROM courses WHERE semester_id = #{semesterId}")
    @ResultMap("courseResultMap")
    List<Course> selectCoursesBySemesterId(@Param("semesterId") String semesterId);

    // --- 新增: 根据学期ID和教师ID查询课程列表 ---
    @Select("SELECT * FROM courses WHERE semester_id = #{semesterId} AND teacher_id = #{teacherId}")
    @ResultMap("courseResultMap")
    List<Course> selectCoursesBySemesterAndTeacher(@Param("semesterId") String semesterId,
                                                   @Param("teacherId") Integer teacherId);

    // --- 新增: 统计某个学期的课程数量 ---
    @Select("SELECT COUNT(*) FROM courses WHERE semester_id = #{semesterId}")
    int countCoursesBySemesterId(@Param("semesterId") String semesterId);

    // --- 新增: 根据课程ID列表和学期ID查询课程 ---
    @Select({"<script>",
            "SELECT * FROM courses",
            "WHERE semester_id = #{semesterId}",
            "<if test='courseIds != null and courseIds.size() > 0'>",
            "AND course_id IN",
            "<foreach collection='courseIds' item='courseId' open='(' separator=',' close=')'>",
            "#{courseId}",
            "</foreach>",
            "</if>",
            "ORDER BY created_at DESC",
            "</script>"})
    @ResultMap("courseResultMap")
    List<Course> findByIdsAndSemester(@Param("courseIds") List<String> courseIds,
                                      @Param("semesterId") String semesterId);
}